package com.mllweb.api.core.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * @Prject: Api
 * @Package: com.mllweb.api.core.json
 * @author: feejson
 * @date: 2017/9/21 11:53
 * @version: V1.0
 */
public class ConvertStrategy extends ObjectMapper {
    public ConvertStrategy() {
        super();
        //允许单引号
        this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        //字段和值都加引号
        this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        //数字不加引号
        this.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, false);
        this.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, false);
        //空值处理为空字符串
        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeString("");
            }
        });
    }
}
